<?php
namespace Profile\Controller;
use Think\Controller;
class BaseController extends Controller {
   
    private $model;
    private $tbl;
    private $perpage;


    public function _initialize(){
    //    $this->model=D('user');
        $this->perpage=5;

        session('Home',1);
        if(!session('?Home')){
            $this->redirect('Home.php/login/from');exit;
        }
}

public function index($tbl,$morder='id desc'){
        
        $this->model=D($tbl);
    //    $data=$this->model->order($model)->select();
      //  $this->assign('list',$data);
      ///  dump($data);
        $count= $this->model->count();
        //dump($this->perpage);exit;
        $Page = new \Think\Page($count,$this->perpage);
        $show = $Page->show();
        $list = $this->model->limit($Page->firstRow.','.$Page->listRows)->select();
       // dump($list);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
}


public function form($tbl){
    $this->display();
}

public function mod($tbl){
     $this->model=D($tbl);
        $id = I('get.id');
        //dump($id);exit;//调试
        //$this->model=M('user');
        $one = $this->model->find($id);
        //dump($one);exit;
        $this->assign('one',$one);
             $this->display();
}


public function add($tbl,$mfield='thumb'){
          $this->model=D($tbl);
            $data = I('post.');
            if (!empty($_FILES[$mfield]['name'])){
            $data[$mfield]=$this->uploadfile($mfield);}
       // dump($_FILES);
     //   $data[$mfield]=$this->uploadfile($mfield);
       // dump($data);exit;
        //
        //$data['whr_username']='';
        //$data['whr_username']=I['post.username'];伪装
       // $this->model = D('user');
        //dump($this->model);exit;//D方法做验证
        if ($this->model->create($data)) {
            $this->model->add();
            $this->success('创建成功！',U('index.php/index/index'));
        }else{
            $this->error('创建失败！'.$this->model->getError());


}}
public function search($tbl,$search_key){
    $this->model=D($tbl);
    $key=I('post.search_key');
        $sql="select * from ydq_user where ydq_username like '%".$key."%'";
        $data=M()->query($sql);
        //M()->ex
        $this->assign('user',$data);
        $this->display();
}


public function del($tbl){
         $this->model=D($tbl);
         $id=I('get.id');
        //$this->model=M('user');
        //$this->model->del($id);
        //M('user')->del(I('get.id'))
        $result=$this->model->delete($id);
        if ($result) {
            $this->success('删除成功',U('Home.php/index/index'));
            # code...
        }
        else{
            $this->error('删除失败');
        }

}
public function update($tbl,$mfield='thumb'){
        $this->model=D($tbl);
        $data=I('post.');
        //dump($data);exit;
        //dump($_FILES['cq_photo']['name']);exit;
        if (!empty($_FILES[$mfield]['name'])){
           /* $photo=$this->model->whrere('id'=.$data['cq_id'])->getField('cq_photo');
            if(is_file()){
                unlink();
            }
            if(is_file()){
                unlink();
            }*/
            $data[$mfield]=$this->uploadfile($mfield);
        }
        else{
            unset($data[$mfield]);

        }

        if ($this->model->create($data)) {
            $this->model->save();//必须有id
            $this->success('更新成功！',U('Home.php/index/index'));
        }
        else{
            $this->error($this->model->getError());
        }
    }


protected function uploadfile($field,$tsize=150){
   
        $config = array(
            'maxSize'    =>   3145728,
            'rootPath'   =>   './Uploads/',
            'savePath'   =>  '',
            'saveName'   => array('uniqid',''),
            'exts'       =>   array('jpg', 'gif', 'png', 'jpeg','txt','word'),

            'autoSub'    =>  true,
            'subName'    => array('date','Ymd'),
        );

     $upload = new \Think\Upload($config);// 实例化上传类
        $upload->saveName = time().'_'.mt_rand();
        // 上传单个文件 
        $info   =   $upload->uploadOne($_FILES[$field]);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
                  $image=new \Think\Image();
            $image->open($config['rootPath'].$info['savepath'].$info['savename']);
            $image->thumb($tsize,$tsize,\Think\Image::IMAGE_THUMB_CENTER)->save($config['rootPath'].$info['savepath'].'thumb_'.$info['savename']);
            return $info['savepath'].'thumb_'.$info['savename'];
        }
    }

    protected function uploadfilee($field){
   
        $config = array(
            'maxSize'    =>   3145728,
            'rootPath'   =>   './Uploads/',
            'savePath'   =>  '',
            'saveName'   => array('uniqid',''),
            'exts'       =>   array('txt','word'),

            'autoSub'    =>  true,
            'subName'    => array('date','Ymd'),
        );

     $upload = new \Think\Upload($config);// 实例化上传类
        $upload->saveName = time().'_'.mt_rand();
        // 上传单个文件 
        $info   =   $upload->uploadOne($_FILES[$field]);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
                 
          
            return $info['savepath'].'thumb_'.$info['savename'];
        }
    }




}
?>